﻿using System;
using System.Threading;
using UnityEditor;
using UnityEngine;

public class ActionFixedItemControl : ActionItemControl
{
    private static float mouseDownOffset = -1f;

    public event ActionFixedItemEventHandler AlterFixedAction;

    public override void HandleInput(DirectorControlState state, Rect trackPosition)
    {
        CinemaActionFixedWrapper action = base.Wrapper as CinemaActionFixedWrapper;
        if (action != null)
        {
            float left = state.TimeToPosition(action.Firetime);
            float num2 = state.TimeToPosition(action.Firetime + action.Duration);
            base.controlPosition = new Rect(left, 0f, num2 - left, trackPosition.height);
            bool flag = this.controlPosition.width < 15f;
            float width = flag ? 0f : 5f;
            Rect position = new Rect(left, 0f, width, trackPosition.height);
            Rect rect2 = new Rect(left + width, 0f, (num2 - left) - (2f * width), trackPosition.height);
            Rect rect3 = new Rect(num2 - width, 0f, width, trackPosition.height);
            EditorGUIUtility.AddCursorRect(rect2, MouseCursor.SlideArrow);
            if (!flag)
            {
                EditorGUIUtility.AddCursorRect(position, MouseCursor.ResizeHorizontal);
                EditorGUIUtility.AddCursorRect(rect3, MouseCursor.ResizeHorizontal);
            }
            base.controlID = GUIUtility.GetControlID(action.Behaviour.GetInstanceID(), FocusType.Passive, base.controlPosition);
            int controlID = GUIUtility.GetControlID(action.Behaviour.GetInstanceID(), FocusType.Passive, position);
            int num5 = GUIUtility.GetControlID(action.Behaviour.GetInstanceID(), FocusType.Passive, rect2);
            int num6 = GUIUtility.GetControlID(action.Behaviour.GetInstanceID(), FocusType.Passive, rect3);
            if (((Event.current.GetTypeForControl(base.controlID) == EventType.MouseDown) && rect2.Contains(Event.current.mousePosition)) && (Event.current.button == 1))
            {
                this.showContextMenu(action.Behaviour);
                Event.current.Use();
            }
            switch (Event.current.GetTypeForControl(num5))
            {
                case EventType.MouseDown:
                    if (rect2.Contains(Event.current.mousePosition) && (Event.current.button == 0))
                    {
                        GUIUtility.hotControl = num5;
                        Selection.activeInstanceID = action.Behaviour.GetInstanceID();
                        mouseDownOffset = ((Event.current.mousePosition.x - state.Translation.x) / state.Scale.x) - action.Firetime;
                        Event.current.Use();
                    }
                    break;

                case EventType.MouseUp:
                    if (GUIUtility.hotControl == num5)
                    {
                        mouseDownOffset = -1f;
                        GUIUtility.hotControl = 0;
                        if (this.AlterFixedAction != null)
                        {
                            this.AlterFixedAction(this, new ActionFixedItemEventArgs(action.Behaviour, action.Firetime, action.Duration, action.InTime, action.OutTime));
                        }
                    }
                    break;

                case EventType.MouseDrag:
                    if (GUIUtility.hotControl == num5)
                    {
                        float num7 = (Event.current.mousePosition.x - state.Translation.x) / state.Scale.x;
                        base.translateItem(base.Track, action, state, num7 - mouseDownOffset);
                    }
                    break;
            }
            switch (Event.current.GetTypeForControl(controlID))
            {
                case EventType.MouseDown:
                    if (position.Contains(Event.current.mousePosition))
                    {
                        GUIUtility.hotControl = controlID;
                        mouseDownOffset = ((Event.current.mousePosition.x - state.Translation.x) / state.Scale.x) - action.Firetime;
                        Event.current.Use();
                    }
                    break;

                case EventType.MouseUp:
                    if (GUIUtility.hotControl == controlID)
                    {
                        mouseDownOffset = -1f;
                        GUIUtility.hotControl = 0;
                    }
                    break;

                case EventType.MouseDrag:
                    if (GUIUtility.hotControl == controlID)
                    {
                        float time = (Event.current.mousePosition.x - state.Translation.x) / state.Scale.x;
                        time = state.SnappedTime(time);
                        if (time >= 0f)
                        {
                            float num9 = action.InTime - (action.Firetime - time);
                            num9 = Mathf.Clamp(num9, 0f, action.ItemLength);
                            float num10 = num9 - action.InTime;
                            action.InTime = num9;
                            action.Firetime += num10;
                            if (this.AlterFixedAction != null)
                            {
                                this.AlterFixedAction(this, new ActionFixedItemEventArgs(action.Behaviour, action.Firetime, action.Duration, action.InTime, action.OutTime));
                            }
                        }
                    }
                    break;
            }
            switch (Event.current.GetTypeForControl(num6))
            {
                case EventType.MouseDown:
                    if (rect3.Contains(Event.current.mousePosition))
                    {
                        GUIUtility.hotControl = num6;
                        mouseDownOffset = ((Event.current.mousePosition.x - state.Translation.x) / state.Scale.x) - action.Firetime;
                        Event.current.Use();
                    }
                    break;

                case EventType.MouseUp:
                    if (GUIUtility.hotControl == num6)
                    {
                        mouseDownOffset = -1f;
                        GUIUtility.hotControl = 0;
                    }
                    break;

                case EventType.MouseDrag:
                    if (GUIUtility.hotControl == num6)
                    {
                        float num11 = (Event.current.mousePosition.x - state.Translation.x) / state.Scale.x;
                        float num12 = state.SnappedTime(num11) - (action.Firetime - action.InTime);
                        Undo.RecordObject(action.Behaviour, string.Format("Changed {0}", action.Behaviour.name));
                        action.OutTime = Mathf.Clamp(num12, 0f, action.ItemLength);
                        if (this.AlterFixedAction != null)
                        {
                            this.AlterFixedAction(this, new ActionFixedItemEventArgs(action.Behaviour, action.Firetime, action.Duration, action.InTime, action.OutTime));
                        }
                    }
                    break;
            }
            if (Selection.activeGameObject == base.Wrapper.Behaviour.gameObject)
            {
                if ((Event.current.type == EventType.ValidateCommand) && (Event.current.commandName == "Copy"))
                {
                    Event.current.Use();
                }
                if ((Event.current.type == EventType.ExecuteCommand) && (Event.current.commandName == "Copy"))
                {
                    DirectorCopyPaste.Copy(base.Wrapper.Behaviour);
                    Event.current.Use();
                }
            }
            if (((Event.current.type == EventType.KeyDown) && (Event.current.keyCode == KeyCode.Delete)) && (Selection.activeGameObject == base.Wrapper.Behaviour.gameObject))
            {
                base.deleteItem(base.Wrapper.Behaviour);
                Event.current.Use();
            }
        }
    }
}

